/*!
 * vinculum
 * Copyright(c) 2017 azzly
 * MIT Licensed
 */
 
'use strict';

class table{
  constructor(table_name){
    this.columns = [];
    this.primary_key = null;
    this.table_name = table_name;
  }
  
  addColumn(a){
    this.columns.push(a);
  }
  
  setPrimaryKey(a){
    this.primary_key = a;
  }
  
  createSql(){
    let collen = this.columns.length;
    let now = 0;
    if(collen>0){
      let sql = ["create table if not exists"];
      sql.push("`" + this.table_name + "`(");
      for(let one of this.columns){
        let len = one.len>0?`(${one.len})`:"";
        sql.push("`" + one.name + "`");
        sql.push(one.type + len);
        if(one.name === this.primary_key){
          sql.push("not null");
        }else{
          sql.push("default");
          sql.push(one.def);
        }
        now++;
        if(now < collen){
          sql.push(",");
        }
      }
      if(this.primary_key){
        sql.push(",primary key(`" + this.primary_key + "`)");
      }
      sql.push(")DEFAULT CHARSET=utf8;");
      return sql.join(" ");
    }else{
      return "";
    }
  }
  
  dropSql(){
    let sql = ["drop table if exists"];
    sql.push("`" + this.table_name + "`(");
    return sql.join(" ");
  }
  
  selectSql(){
    
  }
  
  browseSql(){
    
  }
  
  insertSql(){
    
  }
  
  deleteSql(){
    
  }
}

module.exports = table;